package com.sh.粘包半粘包.two;

import io.vertx.core.Vertx;
import io.vertx.core.net.NetSocket;


/**
 * http 服务器
 */
public class VertxTcpClient {

    public void Start(int port) {
        // 创建Vertx实例
        Vertx vertx = Vertx.vertx();

        vertx.createNetClient().connect(port, "localhost", result -> {
            if (result.succeeded()){  // 连接成功
                System.out.println("连接成功");
                NetSocket socket = result.result();
                for (int i = 0; i < 1000; i++) {
                    socket.write("hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world");
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
                }

                socket.handler(buffer -> {
                    String msg = buffer.getString(0, buffer.length());
                    System.out.println("收到消息:" + msg);
                });

            }else {
                System.out.println("连接失败");
            }

        });
    }

    public static void main(String[] args) {
        new VertxTcpClient().Start(8888);
    }

}
